package com.ruoyi.web.controller.galaxy;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.galaxy.domain.GalaxyUser;
import com.ruoyi.galaxy.service.IGalaxyUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 银河票务用户
 * 
 * @author ruoyi
 */
@Controller
@RequestMapping("/galaxy/user")
public class GalaxyUserController extends BaseController
{
    private final String prefix = "galaxy/user";

    @Resource
    private IGalaxyUserService galaxyUserService;

    @RequiresPermissions("galaxy:user:view")
    @GetMapping()
    public String user()
    {
        return prefix + "/user";
    }

    @PostMapping("/list")
    @RequiresPermissions("galaxy:user:list")
    @ResponseBody
    public TableDataInfo list(GalaxyUser galaxyUser)
    {
        startPage();
        List<GalaxyUser> list = galaxyUserService.selectUserList(galaxyUser);
        return getDataTable(list);
    }

    @Log(title = "银河票务用户", businessType = BusinessType.EXPORT)
    @RequiresPermissions("galaxy:user:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GalaxyUser galaxyUser)
    {
        List<GalaxyUser> list = galaxyUserService.selectUserList(galaxyUser);
        ExcelUtil<GalaxyUser> util = new ExcelUtil<>(GalaxyUser.class);
        return util.exportExcel(list, "银河票务用户");
    }

    /**
     * 新增用户
     */
    @RequiresPermissions("galaxy:user:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户
     */
    @Log(title = "银河票务用户", businessType = BusinessType.INSERT)
    @RequiresPermissions("galaxy:user:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated GalaxyUser user)
    {
        if (!galaxyUserService.checkUserAccountUnique(user))
        {
            return error("新增银河票务用户'" + user.getUserAccount() + "'失败，用户账号已存在");
        }
        user.setCreateBy(getLoginName());
        return toAjax(galaxyUserService.insertUser(user));
    }

    /**
     * 修改用户
     */
    @RequiresPermissions("galaxy:user:edit")
    @GetMapping("/edit/{userId}")
    public String edit(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        mmap.put("user", galaxyUserService.selectUserById(userId));
        return prefix + "/edit";
    }

    /**
     * 修改保存用户
     */
    @Log(title = "银河票务用户", businessType = BusinessType.UPDATE)
    @RequiresPermissions("galaxy:user:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated GalaxyUser user)
    {
        if (!galaxyUserService.checkUserAccountUnique(user))
        {
            return error("修改银河票务用户'" + user.getUserAccount() + "'失败，用户账号已存在");
        }
        user.setUpdateBy(getLoginName());
        return toAjax(galaxyUserService.updateUser(user));
    }

    @Log(title = "银河票务用户", businessType = BusinessType.DELETE)
    @RequiresPermissions("galaxy:user:edit")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        galaxyUserService.deleteUserByIds(ids);
        return success();
    }

    /**
     * 校验用户账号是否唯一
     */
    @PostMapping("/checkUserAccountUnique")
    @ResponseBody
    public boolean checkUserAccountUnique(GalaxyUser galaxyUser)
    {
        return galaxyUserService.checkUserAccountUnique(galaxyUser);
    }

    /**
     * 查询用户订单
     */
    @RequiresPermissions("galaxy:user:order")
    @GetMapping("/order/{userId}")
    public String detail(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        mmap.put("user", galaxyUserService.selectUserById(userId));
        mmap.put("userList", galaxyUserService.selectUserAll());
        return "galaxy/order/order";
    }

}
